// 图片懒加载
/*
  <img ref="cWrap" :data-url="require('./lazyImg/testImg/'+ item +'.png')" v-for="item in arr" :key="item" src="" alt="">
  data-url真正要展示的图片地址
  src最终将data-url的地址替换给src
*/
class LazyImg {
  constructor(scrollEl) {
    this.scrollEl = scrollEl
    this.opera()
  }
  io() {
    return new IntersectionObserver((entires) => {
      entires.forEach(item => {
              // 原图片元素
              let oImg = item.target
              if (item.intersectionRatio > 0 && item.intersectionRatio <= 1) {
                  oImg.setAttribute('src', oImg.getAttribute('data-url'))
              } 
          })
     })
  }
  opera() {
    Array.from(this.scrollEl).forEach(element => {
      this.io().observe(element) 
    })
  }
}

export default LazyImg